﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using Icoders.MarketTracker.Core.MasterData;
using Icoders.MarketTracker.Core.Repository.MasterDataRepositories;
using Icoders.MarketTracker.Core.Validation;
using Icoders.MarketTracker.Web.Lib.Security;
using Icoders.MarketTracker.Web.Lib.Validation;

namespace Icoders.MarketTracker.Web.Areas.Admin.Controllers
{
    [CustomAuthorize(Roles = "ROLE_ADMIN")]
    public class ClientController : Controller
    {
        //
        // GET: /Admin/Client/

       private IClientRepository _clientRepository;
       private IUserRepository _userRepository;
       private IAuditTrailRepository _auditTrailRepository;

        public ClientController(IClientRepository clientRepository, IUserRepository userRepository, IAuditTrailRepository auditTrailRepository)
        {
            _clientRepository = clientRepository;
            _userRepository = userRepository;
            _auditTrailRepository = auditTrailRepository;
        }

        public ActionResult Index()
        {
            return View(_clientRepository.GetAll());
        }
        public ActionResult Create()
        {
            return View(new Client());
        }
        [HttpPost]
        public ActionResult Create(Client model)
        {
            try
            {
                model.Code = "1234";
                model.Description = model.Name;
                _clientRepository.Save(model);
                Saveaudit("Added Client", HttpContext.User.Identity.Name, ActionType.Added);
            }

            catch (DomainValidationException dve)
            {
                ValidationSummary.DisplayDomainValidationResult(dve, ModelState);
                return View(model);
            }
            catch (Exception ex)
            {
                ValidationSummary.DisplayValidationResult(ex.Message, ModelState);
                return View(model);
            }
            return RedirectToAction("Index");
        }
        public ActionResult Edit(int id)
        {
            Client client = _clientRepository.GetById(id);
            return View(client);
        }
        [HttpPost]
        public ActionResult Edit(Client model)
        {
            try
            {
                model.Description = model.Name;
                _clientRepository.Save(model);
                Saveaudit("Edited Client", HttpContext.User.Identity.Name, ActionType.Updated);
            }
            catch (DomainValidationException dve)
            {
                ValidationSummary.DisplayDomainValidationResult(dve, ModelState);
                return View(model);
            }
            catch (Exception ex)
            {
                ValidationSummary.DisplayValidationResult(ex.Message, ModelState);
                return View(model);
            }
            return RedirectToAction("Index");
        }
        public ActionResult Details(int id)
        {
            Client client = new Client();
            client = _clientRepository.GetById(id);
            return View(client);
        }
        public ActionResult Delete(int id)
        {
            _clientRepository.Delete(id);
            Saveaudit("Deleted Client", HttpContext.User.Identity.Name, ActionType.Deleted);
            return RedirectToAction("index");
        }
        public void Saveaudit(string description, string username, ActionType actionType)
        {
            //  private IUserRepository _userRepository;
            //private IAuditTrailRepository _auditTrailRepository;
            // Saveaudit("Changed Password", HttpContext.User.Identity.Name, ActionType.Updated);
            try
            {
                User user = _userRepository.GetByUsername(username);
                AuditTrail auditTrail = new AuditTrail();
                auditTrail.Description = description;
                auditTrail.UserId = user.Id;
                auditTrail.ActionTypeId = Convert.ToInt32(actionType);
                _auditTrailRepository.Save(auditTrail);
            }
            catch (Exception)
            {
                throw;
            }
        }
    }
}
